package cn.yunhe.service.implService;

import cn.yunhe.dao.IreplyDao;
import cn.yunhe.entity.Reply;
import cn.yunhe.service.IreplyService;
import cn.yunhe.util.Paganation;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReplyService implements IreplyService {
    private SqlSession session=SqlSessionFactoryUtil.getSqlSession();
    private IreplyDao rdao=session.getMapper(IreplyDao.class);

    @Override
    public Reply getReplyByRid(int rid) {
        Reply reply=new Reply();
        reply=rdao.getReplyByRid(rid);
        return reply;
    }

    @Override
    public Paganation<Reply> getAllReply(Reply reply, Paganation paganation) {
        Paganation<Reply> paganation1=new Paganation<Reply>();
        Map<String ,Object> map=new HashMap<String, Object>();
        map.put("start",Paganation.getPageSize()*(paganation.getPageNo()-1));
        map.put("ps",Paganation.getPageSize());
        map.put("myreply",reply);
        List<Reply> replyList=rdao.getAllReply(map);


        Map<String ,Object> map1=new HashMap<String ,Object>();
        map1.put("myreply",reply);
        int totalRows=rdao.getCountReply(map1);

        paganation1.setPageNo(paganation.getPageNo());
        paganation1.setTotalRows(totalRows);
        paganation1.setItems(replyList);

        session.close();
        return paganation1;
    }

    @Override
    public boolean addReply(Reply reply) {
        boolean flag=rdao.addReply(reply);
        session.commit();
        session.close();
        return flag;
    }

    @Override
    public boolean updateReply(Reply reply) {
        boolean flag=rdao.updateReply(reply);
        session.commit();
        session.close();
        return flag;
    }

    @Override
    public boolean deleteReply(Reply reply) {
        boolean flag=rdao.deleteReply(reply);
        session.commit();
        session.close();
        return flag;
    }
}
